home *** CD-ROM | disk | FTP | other *** search
- // MakeDir.cmm - CEnvi tool to make a full directory path, given any
- // ver.1 relative path. This goes down the directory tree
- // to create any path necessary. Returns code 1 if path
- // could not be created, or 0 if path was fully created
- // or already exists.
-
- usage()
- {
- puts(`MakeDir.cmm - Make full directory path`)
- puts("\a")
- puts(`USAGE: CEnvi MakeDir <PathSpec>`)
- puts(``)
- puts(`EXAMPLES: CEnvi MakeDir C:\GOOBER\FOOBER\DOOBER`)
- puts(` CEnvi MakeDir ..\BOMP\SHOO\BOMP`)
- puts(``);
- exit(EXIT_FAILURE);
- }
-
- main(argc,argv)
- {
- if( argc!=2 ) usage();
- if( !strcmp(argv[1],"/?") ) usage();
-
- if ( MakeFullDirectoryPath(argv[1]) )
- return EXIT_SUCCESS;
-
- // if made it to here then it failed somewhere
- puts("Path not made\a");
- if ( defined(_WINDOWS_) || defined(_NTWIN_)) && !defined(_SHELL_) {
- printf("press any key to exit...");
- getch();
- }
- if( defined(_NWNLM_) ) PressAnyKeyToContinue();
-
- return ( EXIT_FAILURE );
- }
-
-
- MakeFullDirectoryPath(pPathSpec)
- // make full directories given partial specification. Return True if
- // directory already exists or can be made, else False if it cannot be
- // made, is invalid, contains wildcards, or any other problem
- {
- // failure if any wildcards in the name
- if ( strchr(pPathSpec,'*') || strchr(pPathSpec,'?') )
- return False;
-
- // make full path specification
- if ( !(lFullName = FullPath(pPathSpec)) )
- return False;
-
- // find the first backslash (error if is not one)
- if ( !(lDirDelimiter = strchr(lFullName,'\\')) )
- if( defined(_NWNLM_) && !(lDirDelimiter = strchr(lFullName,'/')) )
- return False;
-
- // if this is at the end of string then this
- // is the root directory, and so make no sub-directories
- if ( lDirDelimiter[1] ) {
-
- // if the final character is a backslash, and if that is
- // not two backslashes in a row, then remove it.
- // some people will add a final backslash on directories
- lLast = strrchr(lDirDelimiter+1,'\\');
- if ( lLast && !lLast[1] && '\\' != lLast[-1] )
- lLast[0] = 0;
-
- if( defined(_NWNLM_) )
- {
- lLast = strrchr(lDirDelimiter+1,'/');
- if ( lLast && !lLast[1] && '/' != lLast[-1] )
- lLast[0] = 0;
- }
- // now make the directories one at a time, not bothering to check
- // if they already exist, because there's no harm making one that
- // already exists
- old = lDirDelimiter;
- while ( lDirDelimiter = strchr(lDirDelimiter+1,'\\') ) {
- lDirDelimiter[0] = 0;
- MakeDirectory(lFullName);
- lDirDelimiter[0] = '\\';
- }
- lDirDelimiter = old;
- while( defined(_NWNLM_) && (lDirDelimiter = strchr(lDirDelimiter+1,'/')) )
- {
- lDirDelimiter[0] = 0;
- MakeDirectory(lFullName);
- lDirDelimiter[0] = '/';
- }
- MakeDirectory(lFullName);
- }
-
- // finally, to see if it all worked, return whether dir exists
- return ( NULL != Directory(lFullName,False,FATTR_SUBDIR,FATTR_SUBDIR) );
- }
-
- MakeDirectory(pDirName) // no return value
- {
- if defined(_OS2_) {
- #define ORD_DOS32CREATEDIR 270
- DynamicLink("doscalls",ORD_DOS32CREATEDIR,BIT32,CDECL,pDirName,0)
- } else if ( defined(_NTCON_) || defined(_NTWIN_) ) {
- DynamicLink("KERNEL32","CreateDirectoryA",STDCALL,pDirName,NULL);
- } else if( defined(_NWNLM_) )
- {
- mkdir(pDirName);
- } else { // dos or windows use the same code
- lReg.ah = 0x39;
- if !defined(_DOS32_)
- lReg.ds = segment(pDirName), lREg.dx = offset(pDirName);
- else
- lReg.dx = pointer(pDirName);
- interrupt(0x21,lReg);
- }
- }
-